package com.service.admin.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.product.controller.rep.ProductLockComboPageRep;
import com.service.admin.product.controller.req.ProductLockComboAddReq;
import com.service.admin.product.controller.req.ProductLockComboPageReq;
import com.service.admin.product.service.ProductLockComboService;
import com.service.admin.utils.MathUtil;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.mapper.CoinsMapper;
import com.service.dao.mapper.ProductLockComboMapper;
import com.service.dao.model.Coins;
import com.service.dao.model.Member;
import com.service.dao.model.ProductLockCombo;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 存币业务 服务接口实现
 *
 * @author 404notfound
 * @since 2021年05月12日12:19:35
 */
@Service
public class ProductLockComboServiceImpl implements ProductLockComboService {

    @Autowired
    private ProductLockComboMapper productLockComboMapper;

    @Autowired
    private CoinsMapper coinsMapper;

    /**
     * 查询所有所有存币业务
     *
     * @param productLockComboPageReq : comboName(产品名称)、comboType(产品类型)、comboStatus(产品状态)
     * @return
     */
    @Override
    public CommonPageRep<ProductLockComboPageRep> pageLockCombo(ProductLockComboPageReq productLockComboPageReq) {
        Assert.notNull(productLockComboPageReq, "参数[productLockComboPageReq]不能为null");
        //设置分页信息
        Page<Member> page = PageHelper.startPage(productLockComboPageReq.getPageNo(), productLockComboPageReq.getPageSize());
        List<ProductLockCombo> productLockCombos = productLockComboMapper.pageLockCombo(productLockComboPageReq.getComboName(),
                productLockComboPageReq.getComboStatus(),
                productLockComboPageReq.getComboType());
        List<ProductLockComboPageRep> pageList = productLockCombos.stream().map(item -> ModelMapperUtil
                .getStrictModelMapper().map(item, ProductLockComboPageRep.class)).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, pageList);
    }

    /**
     * 添加存币业务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ProductLockComboAddReq productLockComboAddReq) {
        //参数效验
        check(productLockComboAddReq);

        //获取年利率
        BigDecimal yearInterestRate = productLockComboAddReq.getYearInterestRate();

        //获取日利率
        BigDecimal dailyInterestRate = MathUtil.div(yearInterestRate, new BigDecimal(36500), 8);

        Coins coins = coinsMapper.selectOne(new LambdaQueryWrapper<Coins>().select(Coins::getCoinName).eq(Coins::getId, productLockComboAddReq.getCoinsId()));
        Date now = new Date();
        ProductLockCombo productLockCombo = new ProductLockCombo();
        ModelMapperUtil.copyNotProperties(productLockComboAddReq,productLockCombo);
        productLockCombo.setCreateTime(now);
        productLockCombo.setUpdateTime(now);
        productLockCombo.setCoinsName(coins.getCoinName());
        productLockCombo.setYearInterestRate(yearInterestRate);
        productLockCombo.setDailyInterestRate(dailyInterestRate);

        productLockComboMapper.insert(productLockCombo);
    }

    /**
     * 参数效验
     */
    private void check(ProductLockComboAddReq productLockComboAddReq) {
        Assert.notNull(productLockComboAddReq, "参数[productLockComboAddReq]不能为null");
        Assert.notNull(productLockComboAddReq.getComboName(), "参数[comboName]不能为null");
        Assert.notNull(productLockComboAddReq.getComboType(), "参数[comboType]不能为null");
        Assert.notNull(productLockComboAddReq.getCoinsId(), "参数[coinsId]不能为null");
        Assert.notNull(productLockComboAddReq.getLockDay(), "参数[lockDay]不能为null");
        Assert.notNull(productLockComboAddReq.getLockLimit(), "参数[lockLimit]不能为null");
        Assert.notNull(productLockComboAddReq.getComboStatus(), "参数[comboStatus]不能为null");
        Assert.notNull(productLockComboAddReq.getYearInterestRate(), "参数[yearInterestRate]不能为null");
    }

    /**
     * 删除存币业务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        Assert.notNull(id, "参数[id]不能为null");
        productLockComboMapper.deleteById(id);
    }

    /**
     * 下架存币业务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void soldOut(Integer id) {
        Assert.notNull(id, "参数[id]不能为null");
        ProductLockCombo productLockCombo = productLockComboMapper.selectById(id);
        productLockCombo.setComboStatus(0);
        productLockComboMapper.updateById(productLockCombo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void soldUp(Integer id) {
        Assert.notNull(id, "参数[id]不能为null");
        ProductLockCombo productLockCombo = productLockComboMapper.selectById(id);
        productLockCombo.setComboStatus(1);
        productLockComboMapper.updateById(productLockCombo);
    }
}
